package com.geo.geotool.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.geo.geotool.entity.geo.GeoFileInfo;
import com.geo.geotool.service.IGeoFileService;
import com.geo.geotool.service.IShpService;
import com.geo.geotool.service.geofile.GeoParserFactory;
import com.geo.geotool.service.geofile.IGeoFileParser;
import com.geo.geotool.service.trans.ICoordinateSystemTrans;
import com.geo.geotool.util.file.Base64File;
import com.geo.geotool.util.file.FileUtils;
import com.geo.geotool.util.trans.EsriGeoJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * • @Description: 地理文件解析
 * • @author  : yell
 * • @date : 2021/11/24 16:24
 */
@Service
@Slf4j
public class GeoFileServiceImpl implements IGeoFileService {
    @Autowired
    IShpService shpService;

    @Value("${tmpPath}")
    String tmpPath;
    @Value("${resultPath}")
    String resultPath;
    @Autowired
    ICoordinateSystemTrans coordinateSystemTrans;

    @Override
    public String parserFileToTxt(String type, String fileValues) throws Exception {
        JSONArray esriJsonArr = new JSONArray();
        GeoFileInfo geoFileInfo = parserFile(type,fileValues);
        JSONArray jsonArray =  geoFileInfo.getGeoJson();
        for(int i = 0 ; i < jsonArray.size() ; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject geometry = jsonObject.getJSONObject("geometry");
            String transformedGeometry = coordinateSystemTrans.coordinateSystemTrans(geometry.toJSONString());
            log.info("transformedGeometry:{}",transformedGeometry);
            /**GeoJSON转EsriJSON*/
            String esriJson = EsriGeoJsonUtil.geo2ersi(transformedGeometry,"");
            JSONObject esriJo = JSONObject.parseObject(esriJson);
            esriJsonArr.add(esriJo);
        }
        Map soeMap = new HashMap();
        soeMap.put("ProcessState",true);
        soeMap.put("ProcessValue",esriJsonArr);
        String resultFileName = UUID.randomUUID().toString().replace("-","")+".txt";
        String filePath = resultPath +File.separator+ resultFileName;
        /**写入txt*/
        FileUtils.writeFile(JSONObject.toJSONString(soeMap),filePath);
        return resultFileName;
    }

    @Override
    public GeoFileInfo parserFile(String type, String fileValues) throws Exception {
        boolean generateFileOk = false;
        JSONArray jsonArray = JSONArray.parseArray(fileValues);
        String tmpName = UUID.randomUUID().toString();
        File geoFile = null;
        /**生成文件*/
        for( int i = 0 ; i < jsonArray.size() ; i++ ){
            JSONObject jObject = jsonArray.getJSONObject(i);
            String suffix = jObject.getString("Format");
            String base64Str = jObject.getString("Value");
            File tmpFile = new File(tmpPath+File.separator+tmpName+"."+suffix);
            generateFileOk = Base64File.generateFle(base64Str,tmpFile);
            if(suffix.equals(type)){
                geoFile = tmpFile;
            }
            if(!generateFileOk){
                /**文件生成失败*/
                break;
            }
        }
        if(generateFileOk){
            IGeoFileParser geoFileParser = GeoParserFactory.getParser(geoFile.getPath());
            GeoFileInfo geoFileInfo = geoFileParser.parserFile(geoFile.getPath());
            return geoFileInfo;
        }
        return null;
    }

}
